import 'package:architecture/os_size.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:two_dimensional_scrollables/two_dimensional_scrollables.dart';

// class TwoDimensionalScrollablePage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     return GetBuilder<TwoDimensionalScrollableController>(
//         init: TwoDimensionalScrollableController(),
//         builder: (controller) {
//           return Scaffold(
//               appBar: AppBar(
//                 title: Text('双向滑动控件'),
//               ),
//               body: TableView.builder(
//                   verticalDetails: ScrollableDetails.vertical(
//                       controller: controller.scrollController),
//                   cellBuilder: _buildCell,
//                   columnCount: 20,
//                   columnBuilder: _buildColumnSpan,
//                   rowCount: 20,
//                   rowBuilder: _buildRowSpan));
//         });
//   }
//
//   _buildCell(BuildContext context, TableVicinity vicinity) {
//     return TableViewCell(
//         child: Center(child: Text('${vicinity.row}行${vicinity.column}列')));
//   }
//
//   _buildColumnSpan(int index) {
//     const TableSpanDecoration decoration = TableSpanDecoration(
//       border: TableSpanBorder(
//         trailing: BorderSide(),
//       ),
//     );
//
//     switch (index % 5) {
//       case 0:
//         return TableSpan(
//             foregroundDecoration: decoration,
//             extent: FixedTableSpanExtent(100),
//             onEnter: (_) => print('Entered column $index'),
//             recognizerFactories: <Type, GestureRecognizerFactory>{
//               TapGestureRecognizer:
//                   GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
//                       () => TapGestureRecognizer(), (TapGestureRecognizer t) {
//                 t.onTap = () => print('Tapped column $index');
//               })
//             });
//       case 1:
//         return TableSpan(
//           foregroundDecoration: decoration,
//           extent: FractionalTableSpanExtent(0.5),
//           onEnter: (_) => print('Entered column $index'),
//           cursor: SystemMouseCursors.contextMenu,
//         );
//       case 2:
//         return TableSpan(
//           foregroundDecoration: decoration,
//           extent: FractionalTableSpanExtent(120),
//           onEnter: (_) => print('Entered column $index'),
//         );
//       case 3:
//         return TableSpan(
//           foregroundDecoration: decoration,
//           extent: FixedTableSpanExtent(145),
//           onEnter: (_) => print('Entered column $index'),
//         );
//       case 4:
//         return TableSpan(
//           foregroundDecoration: decoration,
//           extent: FixedTableSpanExtent(200),
//           onEnter: (_) => print('Entered column $index'),
//         );
//     }
//     throw AssertionError('Invalid index');
//   }
//
//   _buildRowSpan(int index) {
//     final TableSpanDecoration decoration = TableSpanDecoration(
//         color: index.isEven ? Colors.purple[100] : null,
//         border: TableSpanBorder(
//           trailing: BorderSide(width: 3),
//         ));
//
//     switch (index % 3) {
//       case 0:
//         return TableSpan(
//             backgroundDecoration: decoration,
//             extent: FixedTableSpanExtent(50),
//             recognizerFactories: <Type, GestureRecognizerFactory>{
//               TapGestureRecognizer:
//                   GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
//                       () => TapGestureRecognizer(), (TapGestureRecognizer t) {
//                 t.onTap = () => print('Tapped row $index');
//               })
//             });
//       case 1:
//         return TableSpan(
//           backgroundDecoration: decoration,
//           extent: FixedTableSpanExtent(65),
//           cursor: SystemMouseCursors.click,
//         );
//       case 2:
//         return TableSpan(
//           backgroundDecoration: decoration,
//           extent: FractionalTableSpanExtent(0.15),
//         );
//     }
//     throw AssertionError('Invalid index');
//   }
// }

class SimpleTablePage extends StatefulWidget {
  const SimpleTablePage({super.key});

  @override
  State<SimpleTablePage> createState() => _SimpleTablePageState();
}

class _SimpleTablePageState extends State<SimpleTablePage> {
  late final ScrollController _verticalController = ScrollController();
  late final ScrollController _horizontalController = ScrollController();
  int _rowCount = 20;
  int _columnCount = 20;

  @override
  void dispose() {
    _verticalController.dispose();
    _horizontalController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('双向滑动控件'),
      ),
      body: Container(
        width: windowWidth,
        padding: const EdgeInsets.symmetric(horizontal: 50.0),
        child: TableView.builder(
          mainAxis: Axis.horizontal,
          verticalDetails: ScrollableDetails.vertical(
            controller: _verticalController,
          ),
          horizontalDetails:
              ScrollableDetails.horizontal(controller: _horizontalController),
          cellBuilder: _buildCell,
          columnCount: _columnCount,
          columnBuilder: _buildColumnSpan,
          rowCount: _rowCount,
          rowBuilder: _buildRowSpan,
          diagonalDragBehavior: DiagonalDragBehavior.free,
        ),
      ),
      persistentFooterButtons: <Widget>[
        TextButton(
          onPressed: () {
            _horizontalController.jumpTo(0);
          },
          child: const Text('Jump to Left'),
        ),
        TextButton(
          onPressed: () {
            _horizontalController
                .jumpTo(_horizontalController.position.maxScrollExtent);
          },
          child: const Text('Jump to Right'),
        ),
        TextButton(
          onPressed: () {
            setState(() {
              _columnCount += 10;
            });
          },
          child: const Text('Add 10 Columns'),
        ),
        TextButton(
          onPressed: () {
            _verticalController.jumpTo(0);
          },
          child: const Text('Jump to Top'),
        ),
        TextButton(
          onPressed: () {
            _verticalController.jumpTo(
              _verticalController.position.maxScrollExtent,
            );
          },
          child: const Text('Jump to Bottom'),
        ),
        TextButton(
          onPressed: () {
            setState(() {
              _rowCount += 10;
            });
          },
          child: const Text('Add 10 Rows'),
        ),
      ],
    );
  }

  TableViewCell _buildCell(BuildContext context, TableVicinity vicinity) {
    return TableViewCell(
      child: Center(
        child: Text('Tile c: ${vicinity.column}, r: ${vicinity.row}'),
      ),
    );
  }

  TableSpan _buildColumnSpan(int index) {
    const TableSpanDecoration decoration = TableSpanDecoration(
      border: TableSpanBorder(
        trailing: BorderSide(),
      ),
    );

    switch (index % 5) {
      case 0:
        return TableSpan(
          foregroundDecoration: decoration,
          extent: const FixedTableSpanExtent(100),
          onEnter: (_) => print('Entered column $index'),
          recognizerFactories: <Type, GestureRecognizerFactory>{
            TapGestureRecognizer:
                GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
              () => TapGestureRecognizer(),
              (TapGestureRecognizer t) =>
                  t.onTap = () => print('Tap column $index'),
            ),
          },
        );
      case 1:
        return TableSpan(
          foregroundDecoration: decoration,
          extent: const FixedTableSpanExtent(120),
          onEnter: (_) => print('Entered column $index'),
          cursor: SystemMouseCursors.contextMenu,
        );
      case 2:
        return TableSpan(
          foregroundDecoration: decoration,
          extent: const FixedTableSpanExtent(120),
          onEnter: (_) => print('Entered column $index'),
        );
      case 3:
        return TableSpan(
          foregroundDecoration: decoration,
          extent: const FixedTableSpanExtent(145),
          onEnter: (_) => print('Entered column $index'),
        );
      case 4:
        return TableSpan(
          foregroundDecoration: decoration,
          extent: const FixedTableSpanExtent(200),
          onEnter: (_) => print('Entered column $index'),
        );
    }
    throw AssertionError(
      'This should be unreachable, as every index is accounted for in the '
      'switch clauses.',
    );
  }

  TableSpan _buildRowSpan(int index) {
    final TableSpanDecoration decoration = TableSpanDecoration(
      color: index.isEven ? Colors.purple[100] : null,
      border: const TableSpanBorder(
        trailing: BorderSide(
          width: 1,
        ),
      ),
    );

    switch (index % 3) {
      case 0:
        return TableSpan(
          backgroundDecoration: decoration,
          extent: const FixedTableSpanExtent(50),
          recognizerFactories: <Type, GestureRecognizerFactory>{
            TapGestureRecognizer:
                GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
              () => TapGestureRecognizer(),
              (TapGestureRecognizer t) =>
                  t.onTap = () => print('Tap row $index'),
            ),
          },
        );
      case 1:
        return TableSpan(
          backgroundDecoration: decoration,
          extent: const FixedTableSpanExtent(65),
          cursor: SystemMouseCursors.click,
          recognizerFactories: <Type, GestureRecognizerFactory>{
            TapGestureRecognizer:
                GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
              () => TapGestureRecognizer(),
              (TapGestureRecognizer t) =>
                  t.onTap = () => print('Tap row $index'),
            ),
          },
        );
      case 2:
        return TableSpan(
          backgroundDecoration: decoration,
          extent: const FractionalTableSpanExtent(0.15),
          recognizerFactories: <Type, GestureRecognizerFactory>{
            TapGestureRecognizer:
                GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
              () => TapGestureRecognizer(),
              (TapGestureRecognizer t) =>
                  t.onTap = () => print('Tap row $index'),
            ),
          },
        );
    }
    throw AssertionError(
      'This should be unreachable, as every index is accounted for in the '
      'switch clauses.',
    );
  }
}
